<?php declare(strict_types=1);
/**
 * @author      xianganyall <xianganyall@gmail.com>
 * @copyright   2023-2025 owner
 **/

namespace Srv\Libs\Common;

final class CommGps
{
    public static float $pi             = 3.14159265358979323846;       // PI[20位小数]
    public static float $earthRadius    = 6378.137;                     // 地球半径

    /**
     * @param float $srcLat
     * @param float $srcLng
     * @param float $dstLat
     * @param float $dstLng
     * @return int
     * getDistance
     */
    public static function getDistance(float $srcLat, float $srcLng, float $dstLat, float $dstLng):int
    {
        $piNum      = self::$pi / 180;
        $radLatSrc  = $srcLat * $piNum;
        $radLatDes  = $dstLat * $piNum;
        $diffLat    = $radLatSrc - $radLatDes;
        $diffLng    = ($srcLng * $piNum) - ($dstLng * $piNum);
        $distance   = 2 * asin(sqrt(pow(sin($diffLat/2),2) + cos($radLatSrc)*cos($radLatDes)*pow(sin($diffLng/2),2)));
        $distance   = $distance * self::$earthRadius;
        return intval($distance * 1000);
    }

    /**
     * @param float $lat
     * @param float $lng
     * @param int $radius
     * @return array
     * getRange
     */
    public static function getRange(float $lat, float $lng, int $radius):array
    {
        $degree     = (24901 * 1609)/360.0;
        $dpmLat     = 1/$degree;
        $radiusLat  = $dpmLat * $radius;
        $minLat     = $lat - $radiusLat;
        $maxLat     = $lat + $radiusLat;
        $mpdLng     = $degree * cos($lat * (self::$pi / 180));
        $dpmLng     = 1/$mpdLng;
        $radiusLng  = $dpmLng * $radius;
        $minLng     = $lng - $radiusLng;
        $maxLng     = $lng + $radiusLng;
        return ['minLat' => $minLat, 'minLng' => $minLng, 'maxLat' => $maxLat, 'maxLng' => $maxLng];
    }
}